package com.xxy.Main;

import java.util.*;

/**
 * @author xxy
 * @create 2020 11 22 22:14
 */
public class Main{
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        int T =scanner.nextInt();
        int t=1;
        while (true){
            int M=scanner.nextInt();
            int N=scanner.nextInt();
            int[][] arr = new int[M][N];
            for(int i =0;i<M;i++){
                for(int j =0;j<N;j++){
                    arr[i][j] = scanner.nextInt();
                }
            }//输入数据
            System.out.println("Case #"+t+++":");

            //水平处理
            for(int i =0;i<M;i++){
                for(int j =N-1;j>=0;j--){
                    System.out.print(arr[i][j]+" ");
                }
                System.out.println();
            }
            System.out.println();
            //System.out.println("==========");
            //反色
            for(int i =0;i<M;i++){
                for(int j =0;j<N;j++){
                    int a=255-arr[i][j];
                    System.out.print(a+" ");
                }
                System.out.println();
            }
           System.out.println();
            //System.out.println("==========");
            //逆时针旋转
            int i;
            int b = 0;
            for(i =N-1;i>=0;i--){
                for(int j=0;j<M;j++){
                    b=arr[j][i];
                    System.out.print(b+" ");
                }
                //if(b!=arr[M-1][0]){
                    System.out.println(" ");
                //}
            }
            T--;
            if(T<=0){
                break;
            }
        }
    }
}